using Xunit;
using YAPL.Language.Ast;

namespace YAPL.Test.YAPL.Language.Ast {

	public class BinaryExpressionTest {
		[Fact]
		public void NodeEqualsShouldWorkCorrectly() {
			var bin1 = Expressions.Binary(Expressions.Variable("a"), Expressions.Variable("b"), BinaryOperator.Plus);
			var bin2 = Expressions.Binary(Expressions.Variable("a"), Expressions.Variable("b"), BinaryOperator.Minus);
			var bin3 = Expressions.Binary(Expressions.Variable("c"), Expressions.Variable("d"), BinaryOperator.Plus);

			Assert.False(bin1.NodeEquals(null));
			Assert.False(bin1.NodeEquals(bin2));
			Assert.False(bin1.NodeEquals(Expressions.StringLiteral("literal")));
			Assert.True(bin1.NodeEquals(bin3));
		}

		[Fact]
		public void RewriteChildSHouldWorkCorrectly() {
			var a = Expressions.Variable("a");
			var b = Expressions.Variable("b");
			var bin = Expressions.Binary(a, b, BinaryOperator.Plus);
			var r1 = bin.RewriteChild(b, Expressions.Integer(1));
			var r2 = bin.RewriteChild(a, Expressions.Variable("c"));

			Assert.True(r1);
			Assert.True(r2);

			Assert.IsType(typeof(ConstantExpression), bin.Right);
			var i1 = (ConstantExpression) bin.Right;
			Assert.Equal(1, (int)i1.Value);

			Assert.IsType(typeof(VariableExpression), bin.Left);
			var c = (VariableExpression)bin.Left;
			Assert.Equal("c", c.Name);
		}
	}
}
